package com.xjy.headfirst.状态模式;

import lombok.Data;

/**
 * decription:
 *
 * @author 辛纪元
 * @since 2022-07-20
 */

@Data
public class GumballMachine {

    State soldOutState;
    State noQuarterState;
    State hasQuarterState;
    State soldState;
    State state = soldOutState;
    State winnerState;
//    static final int SOLD_OUT = 0;
//
//    static final int NO_QUARTER = 1;
//
//    static final int HAS_QUARTER = 2;
//
//    static final int SOLD = 3;
//
//    int state = SOLD_OUT;

    int count;

    public GumballMachine(int count) {
        soldOutState = new SoldOutState(this);
        noQuarterState = new NoQuarterState(this);
        hasQuarterState = new HasQuarterState(this);
        soldState = new SoldState(this);
        winnerState = new WinnerState(this);
        this.count = count;
        if (count > 0) {
            state = noQuarterState;
        }
    }

    public void trunCrank() {
        state.turnCrank();
        state.dispense();
    }

    public void releaseBall() {
        if (count > 0) {
            count = count - 1;
        }
    }



}
